package com.by.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.by.entity.Goods;
import com.by.mapper.AttachmentMapper;
import com.by.mapper.GoodsMapper;
import com.by.service.GoodsService;
import com.by.util.Result;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 黄佳辉
 * @since 2023-01-04
 */
@RestController
@RequestMapping("/v1/goods")
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    @Autowired
    AttachmentMapper attachmentMapper;

    @PostMapping("/save")
    public Result<String> save(@RequestBody Goods goods){
        Boolean rs=goodsService.saveWithPic(goods);
        return rs ? Result.success("保存成功") : Result.fail("保存失败");
    }

    @PutMapping("/update")
    public Result update(@RequestBody Goods goods){
        Boolean rs=goodsService.updateWithPic(goods);
        return rs ? Result.success("修改成功") : Result.fail("修改失败");
    }

    @GetMapping("/query")
    public Result<Page<Goods>> query(Integer page, Integer pageSize){
        Page<Goods> goodsList =goodsService.queryByPage(page, pageSize);
        return Result.success("商品列表",goodsList);
    }

    @DeleteMapping("/delete/{id}")
    public Result<String> delete(@PathVariable String id){
        boolean deleteResult = goodsService.removeById(id);
        return deleteResult ? Result.success("删除成功") : Result.fail("删除失败");

    }

}
